//
//  BaseMessageCell.swift
//  test-ios
//
//  Created by wyz5204 on 2023/7/26.
//

import Foundation
import UIKit
import Reusable

// 基础的Cell 该 cell 包含一个Model
class BaseMessageCell : AbstractTableViewCell<ChatMessageBean> , Reusable{
    
    /// 设置消息的头像
    lazy var headerView = {
        return UIImageView().then { view in
            view.layer.cornerRadius = 8 // 设置圆角
            view.layer.masksToBounds = true
            view.contentMode = .scaleAspectFill
        }
    }()
    
    lazy var timeView = {
        return UILabel().then { view in
            view.font = UIFont.systemFont(ofSize: 12)
        }
    }()
    
    
    lazy var containerView = {
        return UIButton()
    }()
    
    
    /// 消息的昵称
    /// 消息的时间
    /// 已读和未读
    ///
    
    override func initBinder(_ view: UITableView, _ data: ChatMessageBean, _ index: IndexPath) {
        super.initBinder(view, data, index)
        
        // 设置头像
        headerView.image = UIImage(named: "im_group_icon")
        // 设置时间
        timeView.setTitleAndColor(Date(timeIntervalSince1970: TimeInterval(data.timestamp / 1000)).formattedDate)
        
    }
    
}

// MARK: - 发送消息的cell 的 父cell
class BaseSendMessageCell : BaseMessageCell {
    
    
    override func initView() {
        super.initView()
        headerView.addTo(self.contentView) { view in
            view.snp.makeConstraints { make in
                make.top.equalToSuperview().offset(10)
                make.right.equalToSuperview().offset(-10)
                make.width.height.equalTo(44)
            }
        }
        
        timeView.addTo(self.contentView) { view in
            view.snp.makeConstraints { make in
                make.top.equalTo(headerView.snp.top)
                make.right.equalTo(headerView.snp.left).offset(-5)
            }
        }
        containerView.addTo(self.contentView) { view in
            view.snp.makeConstraints { make in
                make.top.equalTo(timeView.snp.bottom).offset(5)
                make.right.equalTo(timeView.snp.right)
                make.bottom.equalToSuperview().offset(-10)
                make.left.equalToSuperview().offset(10)
            }
        }
    }
    
    override func initBinder(_ view: UITableView, _ data: ChatMessageBean, _ index: IndexPath) {
        super.initBinder(view, data, index)
    }
    
}

// MARK: - 接收消息的cell 的 父cell
class BaseReceiveMessageCell : BaseMessageCell {
    
    override func initView() {
        super.initView()
        headerView.addTo(self.contentView) { view in
            view.snp.makeConstraints { make in
                make.top.equalToSuperview().offset(10)
                make.left.equalToSuperview().offset(10)
                make.width.height.equalTo(44)
            }
        }
        timeView.addTo(self.contentView) { view in
            view.snp.makeConstraints { make in
                make.top.equalTo(headerView.snp.top)
                make.left.equalTo(headerView.snp.right).offset(5)
            }
        }
        containerView.addTo(self.contentView) { view in
            view.snp.makeConstraints { make in
                make.top.equalTo(timeView.snp.bottom).offset(5)
                make.left.equalTo(timeView.snp.left)
                make.bottom.equalToSuperview().offset(-10)
                make.right.equalToSuperview().offset(-10)
            }
        }
    }
    
    override func initBinder(_ view: UITableView, _ data: ChatMessageBean, _ index: IndexPath) {
        super.initBinder(view, data, index)
    }
}

// MARK: - 系统消息/撤回消息的的cell 的 父cell
class BaseCenterMessageCell : BaseMessageCell {
    
    override func initBinder(_ view: UITableView, _ data: ChatMessageBean, _ index: IndexPath) {
        super.initBinder(view, data, index)
    }
}

